package com.wugh.fast.module.mapper.conf;

import com.wugh.fast.module.mapper.filter.CustomPluginsFilter;
import com.wugh.fast.module.mapper.filter.DynamicSchemaFilter;
import com.wugh.fast.module.mapper.interceptor.CommonMapperInterceptor;
import com.wugh.fast.module.mapper.interceptor.DynamicSchemaAspect;
import com.wugh.fast.module.mapper.service.DynamicSchemaService;
import com.wugh.fast.module.mapper.service.impl.RequestParameterSchemaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 说明:
 *
 * @author wugh
 * @date 2019-01-30 15:46
 */
@Configuration
public class MapperAutoConfiguratation {
    @Autowired(required = false)
    private DynamicSchemaService dynamicSchemaService;

    @Bean
    public FilterRegistrationBean<CustomPluginsFilter> getCustomPluginsFilter() {
        FilterRegistrationBean<CustomPluginsFilter> bean = new FilterRegistrationBean();
        bean.setFilter(new CustomPluginsFilter());
        bean.addUrlPatterns("/*");
        return bean;
    }

    @Bean
    public FilterRegistrationBean<DynamicSchemaFilter> getDynamicSchemaFilter() {
        FilterRegistrationBean<DynamicSchemaFilter> bean = new FilterRegistrationBean();
        DynamicSchemaService dynamicSchemaService;
        if (this.dynamicSchemaService == null) {
            dynamicSchemaService = new RequestParameterSchemaService();
        } else {
            dynamicSchemaService = this.dynamicSchemaService;
        }
        DynamicSchemaFilter filter = new DynamicSchemaFilter(dynamicSchemaService);
        bean.setFilter(filter);
        bean.addUrlPatterns("/*");
        return bean;
    }

    @Bean
    public CommonMapperInterceptor getCommonMapperInterceptor() {
        return new CommonMapperInterceptor<>();
    }

    @Bean
    public DynamicSchemaAspect getDynamicSchemaAspect() {
        return new DynamicSchemaAspect();
    }

}
